// moment.js
// 计算连个时间格式间相差多少天
import moment from 'moment'

// 格式化时间
export const formatDate = (date, format = 'YYYY-MM-DD HH:mm:ss') => {
  if (!date) {
    return ''
  }
  return moment(date).format(format)
}
// 比较两个日期零点时间的大小，返回布尔值，可指定类型 大于、小于、等于 大于等于、小于等于
export const compareDate = (date1, date2, type = '==') => {
  if (!date1 || !date2) {
    return false
  }
  let momentDate1 = moment(date1).startOf('day')
  let momentDate2 = moment(date2).startOf('day')
  switch (type) {
    case '==':
      return momentDate1.isSame(momentDate2)
    case '>':
      return momentDate1.isAfter(momentDate2)
    case '<':
      return momentDate1.isBefore(momentDate2)
    case '>=':
      return momentDate1.isSameOrAfter(momentDate2)
    case '<=':
      return momentDate1.isSameOrBefore(momentDate2)
    default:
      return momentDate1.isSame(momentDate2)
  }
}

// 比较两个日期时间的大小，返回布尔值，可指定类型 大于、小于、等于 大于等于、小于等于
export const compareDateTime = (date1, date2, type = '==') => {
  if (!date1 || !date2) {
    return false
  }
  let momentDate1 = moment(date1)
  let momentDate2 = moment(date2)
  switch (type) {
    case '==':
      return momentDate1.isSame(momentDate2)
    case '>':
      return momentDate1.isAfter(momentDate2)
    case '<':
      return momentDate1.isBefore(momentDate2)
    case '>=':
      return momentDate1.isSameOrAfter(momentDate2)
    case '<=':
      return momentDate1.isSameOrBefore(momentDate2)
    default:
      return momentDate1.isSame(momentDate2)
  }
}

// 计算两个时间格式间相差多少天
export const diffDays = (start, end) => {
  let startMoment = moment(start)
  let endMoment = moment(end)
  return endMoment.diff(startMoment, 'days')
}

// 判断时间是否在指定时间段内,返回布尔值 type='[]'表示包含开始时间和结束时间,type='()'表示不包含开始时间和结束时间,type='[)'表示包含开始时间和不包含结束时间，type='(]'表示不包含开始时间和包含结束时间
export const isBetween = (time, start, end, type = '[]') => {
  let timeMoment = moment(time)
  let startMoment = moment(start)
  let endMoment = moment(end)
  return timeMoment.isBetween(startMoment, endMoment, null, type)
}

// 获取指定时间的前几天或后几天的时间
export const getDate = (date, days, format = 'YYYY-MM-DD HH:mm:ss', units = 'days') => {
  let momentDate = moment(date)
  return momentDate.add(days, units).format(format)
  // return momentDate.add(days, 'days').format(format)
}

// 获取指定时间的星期几，返回周一到周日的文字
export const getWeekday = (date) => {
  let weekday = moment(date).weekday()
  switch (weekday) {
    case 0:
      return '星期日'
    case 1:
      return '星期一'
    case 2:
      return '星期二'
    case 3:
      return '星期三'
    case 4:
      return '星期四'
    case 5:
      return '星期五'
    case 6:
      return '星期六'
    default:
      return '星期日'
  }
}
// 获取指定日期的名称 今天、明天、后天、昨天、前天 等
export const getDateName = (date) => {
  let momentDate = moment(date)
  // 获取今日日期
  let now = moment().startOf('day')
  let diff = momentDate.diff(now, 'days')
  if (diff === 0) {
    return '今天'
  } else if (diff === 1) {
    return '明天'
  } else if (diff === 2) {
    return '后天'
  } else if (diff === -1) {
    return '昨天'
  } else if (diff === -2) {
    return '前天'
  } else {
    return momentDate.format('YYYY-MM-DD')
  }
}

// 获取指定日期范围内的日期数组
export const getDateRange = (start, end) => {
  let startMoment = moment(start)
  let endMoment = moment(end)
  let dateArray = []
  while (startMoment.isBefore(endMoment)) {
    dateArray.push(startMoment.format('YYYY-MM-DD'))
    startMoment.add(1, 'days')
  }
  dateArray.push(endMoment.format('YYYY-MM-DD'))
  return dateArray
}

//判断时间段是否有重叠，返回布尔值
export const isOverlap = (start1, end1, start2, end2) => {
  let start1Moment = moment(start1)
  let end1Moment = moment(end1)
  let start2Moment = moment(start2)
  let end2Moment = moment(end2)
  return start1Moment.isBetween(start2Moment, end2Moment) || end1Moment.isBetween(start2Moment, end2Moment) || start2Moment.isBetween(start1Moment, end1Moment) || end2Moment.isBetween(start1Moment, end1Moment)
}

// 获取起始日期和结束日期，本日、本周、本月、本季度、本年的开始日期和结束日期
export const getStartAndEnd = (type,format = 'YYYY-MM-DD HH:mm:ss') => {
  let now = moment()
  switch (type) {
    case 'today':
      return {
        start: now.startOf('day').format(format),
        end: now.endOf('day').format(format)
      }
    case 'week':
      // 周一做为一周的开始
      return {
        start: now.startOf('week').add(1, 'days').format(format),
        end: now.endOf('week').add(1, 'days').format(format)
      }
    case'month':
      return {
        start: now.startOf('month').format(format),
        end: now.endOf('month').format(format)
      }
    case 'year':
      return {
        start: now.startOf('year').format(format),
        end: now.endOf('year').format(format)
      }
    default:
      return {
        start: now.startOf('day').format(format),
        end: now.endOf('day').format(format)
      }
  }
}


